package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.atguigu.gmall.pms.vo.AttrGroupVo;
import com.atguigu.gmall.pms.vo.ItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 属性分组
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2022-04-18 11:54:13
 */
@Api(tags = "属性分组 管理")
@RestController
@RequestMapping("pms/attrgroup")//http://api.gmall.com/pms/attrgroup/category/225
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;
    //根据spuid查询spu的规格参数组 和属性对象
    @GetMapping("queryItemAttrGroupsBySpuId/{spuId}")
    public ResponseVo<List<ItemAttrGroupVo>> queryItemAttrGroupsBySpuId(@PathVariable("spuId") Long spuId){
        List<ItemAttrGroupVo> itemAttrGroupVos = attrGroupService.queryItemAttrGroupsBySpuId(spuId);
        return ResponseVo.ok(itemAttrGroupVos);
    }
    //http://api.gmall.com/pms/attrgroup/withattrs/225
    //使用categoryid查询它所有的属性分组和每个分组的属性集合
    @GetMapping("withattrs/{categoryId}")
    public ResponseVo<Object> queryAttrGroupAndAttrsByCategoryId(@PathVariable("categoryId")Long categoryId){
        List<AttrGroupVo>  attrGroupVos = attrGroupService.queryAttrGroupAndAttrsByCategoryId(categoryId);
        return ResponseVo.ok(attrGroupVos);
    }


    //当前请求一定是 操作 pms_attr_group  ,传入的参数是categoryid   使用cid查询属性分组
    //category/225
    @GetMapping("category/{categoryId}")
    public ResponseVo<List<AttrGroupEntity>> queryAttrGroupsByCategoryId(@PathVariable("categoryId")Long categoryId){
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, categoryId));
        return ResponseVo.ok(attrGroupEntities);
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrGroupByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = attrGroupService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrGroupEntity> queryAttrGroupById(@PathVariable("id") Long id){
		AttrGroupEntity attrGroup = attrGroupService.getById(id);

        return ResponseVo.ok(attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		attrGroupService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
